import {g_UserList} from './g_UserList'
import {User} from './user'
import {Base64} from './base64'
import{xchat_ws} from '@/utils/xchat/xchat_ws'
import store from '@/store'
function member_in_out_hint(appdata,type,xMessager)
{
	appdata.type = type;
	if(xMessager.level > 100 && xchat_ws.publicChat){
		xchat_ws.publicChat.disinout(appdata);
	}
}
let loadingTimer = null
export function xchat_swf_message(type,param,xMessager){
	console.log('xchat_swf_message. ' + type);
	console.log(param);
	try{
		switch (type){
			case 'MSG_LOGIN_ACK':
				xMessager.LoginACK(param);
				break;
			case 'MSG_REENTER':
				xMessager.ReEnter();
				break;
			case 'MSG_SESSION_PARAM':
				xMessager.Logined = true;
				break;
			case 'MSG_SOMEBODY_IN':
				console.log('MSG_SOMEBODY_IN OK.');
				var appdata = JSON.parse(Base64.decode(param.appdata));
				appdata.nickname = Base64.decode(appdata.nickname_b64);
				var user = new User(param.uid,param.level,appdata,xMessager);
				g_UserList.UserIn(user);
				if (param.in_out_hint){
					member_in_out_hint(appdata,1,xMessager);
				}
				break;
			case 'MSG_ROOMHOT':
				store.dispatch('setAllCount',param.roomhot)
				break;
			case 'MSG_SOMEBODY_OUT':
				var u = g_UserList.GetUser(param.uid)
				if (param.in_out_hint && u){
					member_in_out_hint(u.appdata,0,xMessager);
				}
				g_UserList.UserOut(param.uid);
				break;
			case 'MSG_SOMEBODY_CHANGELEVEL':
				var u = g_UserList.GetUser(param.uid)
				var appdata = u.appdata;
				g_UserList.UserOut(param.uid);
				appdata.nickname = Base64.decode(u.appdata.nickname_b64);
				appdata.levelinroom=param.level;
				var user = new User(param.uid,param.level,appdata);
				g_UserList.UserIn(user);
				break;
			case 'MSG_SOMEBODY_CHANGEAPPDATA':
				var appdata = JSON.parse(Base64.decode(param.appdata));
				var u = g_UserList.GetUser(param.uid)
				g_UserList.UserOut(param.uid);
				appdata.nickname = Base64.decode(appdata.nickname_b64);
				console.log(appdata);
				var user = new User(param.uid,param.level,appdata);
				g_UserList.UserIn(user);
				break;
			case 'MSG_SOMEBODY_CHATENABLE':
				if (param.uid == xMessager.uid){
					if (param.chatenable){
						xchat_ws.privateChat.common_insert('<span class="warning">你的禁止发言已经解除!</span>')
						xMessager.Chatdisable = 0;
					}
					else{
						let info = '<span class="warning">你被管理员禁止发言5分钟!</span>'
						xchat_ws.privateChat.common_insert(info)
						xMessager.Chatdisable = 1;
					}
				}
				else{
					var u = g_UserList.GetUser(param.uid);
					if (u){
						if (!param.chatenable){
							var s = '<span class="warning">【'+u.appdata.nickname + "】被被管理员禁止发言15分钟!</span>"
							xchat_ws.publicChat.common_insert(s)
						}
					}
				}
				break;
			case 'MSG_KICKOUT':
				if (param.uid == xMessager.uid){
					xchat_ws.privateChat.common_insert('<span class="warning">你被管理员踢出了！1小时内不允许进入本房间！</span>')
					setTimeout(function(){
						location.reload(true);
					},5000);

				}
				else{
					var u = g_UserList.GetUser(param.uid);
					if (u){
						var s = '<span class="warning">【'+u.appdata.nickname +'('+ u.appdata.uid+")】被管理员踢出房间了。1小时内不允许进入本房间。</span>";
						xchat_ws.publicChat.common_insert(s)
					}
				}
				break;
			case 'MSG_MESSAGE':
				console.log(Base64.decode(param.chatdata));
				var data = JSON.parse(Base64.decode(param.chatdata));
				xMessager.OnData(data);
				break;
			case 'MSG_TOKEN_INFO':
				if (param.tokenid==0){
					if (param.tokenstatus == 1){
						let tokenop_oper_appdata = JSON.parse(Base64.decode(param.tokenop_oper_appdata))
						let hasUser = g_UserList.GetUser(tokenop_oper_appdata.uid)
						if(!hasUser && store.getters.roompara.set_relay_room === 1){
							let user = new User(tokenop_oper_appdata.uid,tokenop_oper_appdata.levelinroom,tokenop_oper_appdata,xMessager)
						}
						let SET_MICTIME = new Date().getTime()
						Emitter.trigger('SET_MICTIME',SET_MICTIME)
						var i = 0;
						store.commit('SET_ROOMLIVE',true)
						var status = function(){
							var url_data = JSON.parse(Base64.decode(param.tokenop_comment));
							Emitter.trigger('shangmai',url_data)
						}
						if((store.getters.roompara.login_alert_time) || store.getters.xmessager.level >= 100) {
							if(store.getters.roomChange){
								if(/(Android)/i.test(navigator.userAgent)){
									store.commit('SET_MICINDEX',100);
									store.commit('SET_MICLOADING',true);
									status();
								}else{
									store.commit('SET_MICLOADING',true);
									loadingTimer = setInterval(function(){
										i++;
										store.commit('SET_MICINDEX',i);
										if(i == 100){
											clearInterval(loadingTimer);
											loadingTimer = null;
											i = 0;
											store.commit('SET_MICINDEX',i);
											store.commit('SET_MICLOADING',false);
											status();
										}
									},100);
								}
							}
						}
						store.commit('SET_ROOMCHANGE',true);
						let speaker = Base64.decode(tokenop_oper_appdata.nickname_b64);
						store.commit('SET_SPEAKER',speaker);
					}
					else{
						store.commit('SET_MICLOADING',false);
						store.commit('SET_MICINDEX',0);
						store.commit('SET_ROOMLIVE',false);
						store.commit('SET_SPEAKER',"");
						store.commit('SET_MICTIME',0)
						if(store.getters.player){
							store.getters.player.destroy()
							store.commit('SET_PLAYER',null)
						}
						if(loadingTimer){
							clearInterval(loadingTimer);
						}
						Emitter.trigger('xiamai');
					}
				}
				break;
			case 'MSG_TOKEN_REQUEST':
				console.log('MSG_TOKEN_REQUEST');
				console.log(param);
				var u = g_UserList.GetUser(param.uid);
				if(param.type){
					let info = u.appdata.nickname+"排麦成功"
					xchat_ws.publicChat.common_insert(info,1)
				}else{
					let info = u.appdata.nickname+"离开麦序列表"
					xchat_ws.publicChat.common_insert(info,1)
				}

				break;
			case 'MSG_ROOM_REINIT':
						store.commit('SET_MICLOADING',false);
						store.commit('SET_MICINDEX',0);
						store.commit('SET_ROOMLIVE',false);
						store.commit('SET_SPEAKER',"");
						store.commit('SET_MICTIME',0)
						if(store.getters.player){
							store.getters.player.destroy()
							store.commit('SET_PLAYER',null)
						}
						if(loadingTimer){
							clearInterval(loadingTimer);
						}
						Emitter.trigger('xiamai');
				break;
		}
	}catch(e){
		console.log(e);
	}
}
